import os,random
import tkinter as tk
from tkinter import filedialog, messagebox

# 用户名密码（简单模拟，生产环境应使用数据库）
USERNAME = "admin"
PASSWORD = "123456"

# 创建 Tkinter 窗口
root = tk.Tk()
root.title("Tkinter 文件处理系统")
root.geometry("400x300")


    
def format_number_four_chars(number):
    return "{:04d}".format(number)

def get_file_paths(directory):
    file_paths = []
    for root, dirs, files in os.walk(directory):
        for file in files:
            file_paths.append(os.path.join(root, file))
    return file_paths

# 选择文件夹并处理
def select_folder():
    folder_path = filedialog.askdirectory()
    files_path = get_file_paths(folder_path)
    for file_path in files_path:
        if '.db' in file_path:
            os.remove(file_path)
            continue
        name_old=os.path.basename(file_path)
        name_new='IMG_'+format_number_four_chars(random.randint(1,9999))+'.'+name_old.split('.')[1]
        new_path = os.path.join(os.path.dirname(file_path), name_new)
        os.rename(file_path, new_path)

# 登录验证
def login():
    user = entry_user.get()
    pwd = entry_pwd.get()
    
    # if user == USERNAME and pwd == PASSWORD:
    #     messagebox.showinfo("登录成功", "欢迎使用文件处理工具！")
    #     login_frame.pack_forget()
    #     main_frame.pack()
    # else:
    #     messagebox.showerror("登录失败", "用户名或密码错误")
    login_frame.pack_forget()
    main_frame.pack()

# 创建登录界面
login_frame = tk.Frame(root)
login_frame.pack(pady=50)

tk.Label(login_frame, text="用户名:").grid(row=0, column=0)
entry_user = tk.Entry(login_frame)
entry_user.grid(row=0, column=1)

tk.Label(login_frame, text="密码:").grid(row=1, column=0)
entry_pwd = tk.Entry(login_frame, show="*")
entry_pwd.grid(row=1, column=1)

btn_login = tk.Button(login_frame, text="登录", command=login)
btn_login.grid(row=2, columnspan=2, pady=10)

# 创建主界面
main_frame = tk.Frame(root)
btn_select_folder = tk.Button(main_frame, text="选择文件夹", command=select_folder)
btn_select_folder.pack(pady=20)

# 运行 Tkinter 窗口
root.mainloop()
